new Vue({
    el:"#app",
    data: {
        fullscreenLoading:false,
        screenLoading:false,
        dialogDeleteVisible:false,
        id:null,
        userData:[],
        queryContent:{
            keyword:'',
            date:[]
        },
        pageNo: 1,
        pageSize: 20,
        total: 0,
        pages: 0
    },
    mounted:function(){
        var _this=this;
        _this.loadUserList();
    },
    methods:{
        trim:function(str){
            if(isNaN(str)){
                return str.replace(/(^\s*)|(\s*$)/g, "");
            }else{
                return str
            }
        },
        loadUserList:function () {
            var _this = this;
            _this.screenLoading=true;
            var startDate = '';
            var endDate = '';
            if(_this.queryContent.date != null && _this.queryContent.date[0] != null) {
                startDate = _this.easyFormatDate(_this.queryContent.date[0]);
                endDate = _this.easyFormatDate(_this.queryContent.date[1]);
            }
            axios.get(request_prefix+"/cash/user/list",{
                params: {
                    pageSize: _this.pageSize,
                    currentPage: _this.pageNo,
                    keyword: _this.queryContent.keyword,
                    startDate: startDate,
                    endDate: endDate
                }
            })
                .then(function(res){
                    if(res.data.code===0){
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.userData = res.data.data.dataList;
                        _this.screenLoading = false;
                        return;
                    }else{
                        _this.screenLoading=false;
                        _this.$message.error("获取数据失败");
                        return;
                    }
                })
                .catch(function(res){
                    _this.screenLoading=false;
                    _this.$message.error("请求失败");
                    return;
                })
        },
        resetQuery:function () {
            var _this = this;
            _this.queryContent.keyword = '';
            _this.queryContent.date = [];
            _this.pageNo = 1;
            _this.loadUserList();
        },
        handleSizeChange:function (val) {
            var _this = this;
            _this.pageSize = val;
            _this.handleCurrentChange(1);
        },
        //分页
        handleCurrentChange:function(val) {
            var _this=this;
            var startDate = '';
            var endDate = '';
            if(_this.queryContent.date != null && _this.queryContent.date[0] != null) {
                startDate = _this.easyFormatDate(_this.queryContent.date[0]);
                endDate = _this.easyFormatDate(_this.queryContent.date[1]);
            }
            axios.get(request_prefix+"/cash/user/list",{
                params: {
                    pageSize: _this.pageSize,
                    keyword: _this.queryContent.keyword,
                    currentPage: val,
                    startDate: startDate,
                    endDate: endDate
                }
            })
                .then(function(res){
                    if(res.data.code===0){
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.userData = res.data.data.dataList;
                    }else{
                        _this.$message.error("获取数据失败")
                    }
                })
                .catch(function(res){
                    _this.$message.error("请求失败")
                })
        },
        easyFormatDate: function(date) {
            var year = date.getFullYear();
            var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1 + "";
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() + "";
            return year + "-" + month + "-" + day;
        },
        outputExcel:function () {
            var _this=this;
            var startDate = '';
            var endDate = '';
            if(_this.queryContent.date != null && _this.queryContent.date[0] != null) {
                startDate = _this.easyFormatDate(_this.queryContent.date[0]);
                endDate = _this.easyFormatDate(_this.queryContent.date[1]);
            }
            window.location.href = request_prefix + "/cash/user/excel?startDate=" + startDate + "&endDate=" + endDate;
        },
        deleteUser:function (id) {
            var _this = this;
            _this.id = id;
            _this.dialogDeleteVisible = true;
        },
        confirmDelete:function () {
            var _this = this;
            var id = parseInt(_this.id);
            var params = new URLSearchParams();
            params.append("id",id);
            axios.post(request_prefix + "/cash/user/delete",params)
                .then(function (res) {
                  if(res.data.code === 0) {
                      _this.loadUserList();
                      _this.dialogDeleteVisible = false;
                  } else {
                      _this.$message.error(res.data.message);
                      _this.dialogDeleteVisible = false;
                  }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                    _this.dialogDeleteVisible = false;
                })
        }
    }
});